home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GeometryCompression / cgview.java.z / cgview.java
Encoding:
Java Source  |  2003-08-08  |  7.0 KB  |  208 lines

  1. /*
  2.  *    @(#)cgview.java 1.15 02/10/21 13:42:58
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.utils.compression.* ;
  41. import com.sun.j3d.utils.behaviors.vp.* ;
  42. import com.sun.j3d.utils.applet.MainFrame ;
  43. import com.sun.j3d.utils.universe.* ;
  44. import javax.media.j3d.* ;
  45. import javax.vecmath.* ;
  46. import java.applet.Applet ;
  47. import java.awt.BorderLayout ;
  48. import java.awt.event.* ;
  49. import java.io.* ;
  50.  
  51. public class cgview extends Applet {
  52.  
  53.     private SimpleUniverse u = null;
  54.  
  55.     public BranchGroup createSceneGraph(CompressedGeometry cg) {
  56.     // Create the root of the branch graph
  57.     BranchGroup objRoot = new BranchGroup() ;
  58.  
  59.         // Create a Transformgroup to scale all objects so they
  60.         // appear in the scene.
  61.         TransformGroup objScale = new TransformGroup() ;
  62.         Transform3D t3d = new Transform3D() ;
  63.         t3d.setScale(0.7) ;
  64.         objScale.setTransform(t3d) ;
  65.         objRoot.addChild(objScale) ;
  66.  
  67.     // Create the transform group node and initialize it to the
  68.     // identity.  Enable the TRANSFORM_WRITE capability so that
  69.     // our behavior code can modify it at runtime.  Add it to the
  70.     // root of the subgraph.
  71.     TransformGroup objTrans = new TransformGroup() ;
  72.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE) ;
  73.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ) ;
  74.     objScale.addChild(objTrans) ;
  75.  
  76.     // Add compressed geometry to the scene graph.
  77.     CompressedGeometryHeader hdr = new CompressedGeometryHeader() ;
  78.     cg.getCompressedGeometryHeader(hdr) ;
  79.  
  80.     // There isn't really enough information in the compressed geometry
  81.     // header to unamiguously determine the proper rendering attributes.
  82.     // The bufferDataPresent field specifies whether or not normals are
  83.     // bundled with vertices, but the compressed buffer can still contain
  84.     // normals that should be lit.  Assume that any surface geometry
  85.     // should be lit and that lines and points should not unless the
  86.     // header contains the NORMAL_IN_BUFFER bit.
  87.     Material m = new Material() ;
  88.     if ((hdr.bufferType == hdr.TRIANGLE_BUFFER) ||
  89.         ((hdr.bufferDataPresent & hdr.NORMAL_IN_BUFFER) == 1))
  90.         m.setLightingEnable(true) ;
  91.     else
  92.         m.setLightingEnable(false) ;
  93.  
  94.     Appearance a = new Appearance() ;
  95.     a.setMaterial(m) ;
  96.  
  97.     objTrans.addChild(new Shape3D(cg, a)) ;
  98.  
  99.     // Create mouse behavior scheduling bounds.
  100.     BoundingSphere bounds =
  101.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0) ;
  102.  
  103.         // Set up the background
  104.         Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
  105.         Background bgNode = new Background(bgColor) ;
  106.         bgNode.setApplicationBounds(bounds) ;
  107.         objRoot.addChild(bgNode) ;
  108.  
  109.         // Set up the ambient light
  110.         Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f) ;
  111.         AmbientLight ambientLightNode = new AmbientLight(ambientColor) ;
  112.         ambientLightNode.setInfluencingBounds(bounds) ;
  113.         objRoot.addChild(ambientLightNode) ;
  114.  
  115.         // Set up the directional lights
  116.         Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f) ;
  117.         Vector3f light1Direction  = new Vector3f(1.0f, 1.0f, 1.0f) ;
  118.         Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f) ;
  119.         Vector3f light2Direction  = new Vector3f(-1.0f, -1.0f, -0.9f) ;
  120.  
  121.         DirectionalLight light1
  122.             = new DirectionalLight(light1Color, light1Direction) ;
  123.         light1.setInfluencingBounds(bounds) ;
  124.         objRoot.addChild(light1) ;
  125.  
  126.         DirectionalLight light2
  127.             = new DirectionalLight(light2Color, light2Direction) ;
  128.         light2.setInfluencingBounds(bounds) ;
  129.         objRoot.addChild(light2) ;
  130.  
  131.     return objRoot ;
  132.     }
  133.  
  134.     private void usage() {
  135.     System.out.println("Usage: cgview <.cg file> <object index>")  ;
  136.     System.exit(0)  ;
  137.     }
  138.  
  139.     public cgview(String args[]) {
  140.         if (args.length < 1)
  141.         usage() ;
  142.  
  143.     int index ;
  144.     if (args.length < 2)
  145.         index = 0 ;
  146.     else
  147.         index = Integer.parseInt(args[1]) ;
  148.  
  149.     String filename = args[0] ;
  150.     if (filename == null)
  151.         usage() ;
  152.  
  153.     // Read the compressed geometry.
  154.     CompressedGeometry cg = null ;
  155.     try {
  156.         CompressedGeometryFile cgf ;
  157.         cgf = new CompressedGeometryFile(filename, false) ;
  158.  
  159.         if (cgf.getObjectCount() == 0) {
  160.         System.out.println("no objects were found in " + filename) ;
  161.         System.exit(0) ;
  162.         }
  163.  
  164.         cg = cgf.read(index) ;
  165.         cgf.close() ;
  166.  
  167.     } catch (IOException e) {
  168.         System.out.println(e) ;
  169.         System.exit(0) ;
  170.     }
  171.  
  172.     setLayout(new BorderLayout()) ;
  173.     Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  174.     add("Center", c) ;
  175.  
  176.     // Create a simple scene and attach it to the virtual universe
  177.     BranchGroup scene = createSceneGraph(cg) ;
  178.     u = new SimpleUniverse(c) ;
  179.  
  180.     // add mouse behaviors to the ViewingPlatform
  181.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  182.  
  183.         // This will move the ViewPlatform back a bit so the
  184.         // objects in the scene can be viewed.
  185.     viewingPlatform.setNominalViewingTransform();
  186.  
  187.     OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
  188.     BoundingSphere bounds = 
  189.       new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  190.     orbit.setSchedulingBounds(bounds);
  191.     viewingPlatform.setViewPlatformBehavior(orbit);
  192.  
  193.     u.addBranchGraph(scene) ;
  194.     }
  195.  
  196.     public void destroy() {
  197.     u.cleanup();
  198.     }
  199.  
  200.     //
  201.     // The following allows cgview to be run as an application
  202.     // as well as an applet.
  203.     //
  204.     public static void main(String[] args) {
  205.     new MainFrame(new cgview(args), 700, 700) ;
  206.     }
  207. }
  208.